import re

from common.BasePages import BasePage


class AgreementPointTable(BasePage):
    # 网关管理-协议配置-点表配置
    def agreement_point_table_url(self):
        self.goto_url("projectManagement/gatewayManagement")

    def create_agreement_point_table(self):
        self.click_more_functions("网关管理", "协议配置")
        self.click_more_functions("协议配置", "点表配置")
        self.page.get_by_role("button", name="新增", exact=True).click()
        self.page.get_by_role("textbox", name="请输入点位名称，40字内").click()
        self.page.get_by_role("textbox", name="请输入点位名称，40字内").fill("协议添加001")
        self.page.get_by_role("textbox", name="请选择寄存器地址区").click()
        self.page.get_by_role("listitem").filter(has_text="4X").click()
        self.page.get_by_role("textbox", name="请选择权限").click()
        self.page.get_by_role("listitem").filter(has_text="R/W").click()
        self.page.get_by_role("textbox", name="前10进制数.后16进制数").click()
        self.page.get_by_role("textbox", name="前10进制数.后16进制数").fill("124")
        self.page.get_by_role("textbox", name="请选择数据类型").click()
        self.page.get_by_role("listitem").filter(has_text=re.compile(r"^Int16 AB$")).click()
        self.page.get_by_role("button", name="确 定").click()
        self.assert_alert_content("新增点位成功，请及时下发点表")

    def edit_agreement_point_table(self):
        self.click_more_functions("网关管理", "协议配置")
        self.click_more_functions("协议配置", "点表配置")
        self.page.locator(
            ".el-table__fixed-body-wrapper > .el-table__virtual-wrapper > div > .el-table__body > tbody > tr > .el-table_27_column_440 > .cell > .basicTableBtnBox > .el-icon-edit-outline").first.click()
        self.page.get_by_role("textbox", name="请输入单位").click()
        self.page.get_by_role("textbox", name="请输入单位").fill("ms")
        self.page.get_by_role("button", name="确 定").click()
        self.assert_alert_content("编辑点位成功，请及时下发点表")

    def delete_agreement_point_table(self):
        self.click_more_functions("网关管理", "协议配置")
        self.click_more_functions("协议配置", "点表配置")
        self.page.locator(
            ".el-table__fixed-body-wrapper > .el-table__virtual-wrapper > div > .el-table__body > tbody > tr > .el-table_27_column_440 > .cell > .basicTableBtnBox > .el-icon-delete").first.click()
        self.page.get_by_role("button", name="确定").click()
        self.assert_alert_content("删除成功")

    def add_agreement_point_table(self):
        # 批量新增
        self.click_more_functions("网关管理", "协议配置")
        self.click_more_functions("协议配置", "点表配置")
        self.page.get_by_role("button", name="批量新增").click()
        self.page.get_by_role("textbox", name="请选择协议").click()
        self.page.get_by_role("list").filter(has_text="Modbus RTU").click()
        self.page.get_by_role("textbox", name="请选择寄存器地址区").click()
        self.page.get_by_role("listitem").filter(has_text="4X").click()
        self.page.get_by_role("textbox", name="请选择数据类型").click()
        self.page.get_by_role("listitem").filter(has_text=re.compile(r"^Int16 BA$")).click()
        self.page.get_by_role("textbox", name="进制数").click()
        self.page.get_by_role("textbox", name="进制数").fill("100")
        self.page.get_by_placeholder("请输入个数").click()
        self.page.get_by_placeholder("请输入个数").fill("10")
        self.page.get_by_role("textbox", name="请选择权限").click()
        self.page.get_by_role("listitem").filter(has_text="R/W").click()
        self.page.get_by_role("button", name="确 定").click()
        self.assert_alert_content("批量新增成功，请及时下发点表")

    def batch_modification(self):
        # 批量修改从站号
        self.click_more_functions("网关管理", "协议配置")
        self.click_more_functions("协议配置", "点表配置")
        self.list_row(2)
        # page.get_by_role("row", name="2", exact=True).locator("span").nth(1).click()
        self.page.get_by_role("button", name="批量修改从站号").click()
        self.page.get_by_role("dialog", name="批量修改从站号").get_by_placeholder("请输入从站号").click()
        self.page.get_by_role("dialog", name="批量修改从站号").get_by_placeholder("请输入从站号").fill("2")
        self.page.get_by_role("button", name="确 定").click()
        self.assert_alert_content("批量修改从站号成功")
